package com.jeehentor.system.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeehentor.annotation.SystemLogAnnotation;
import com.jeehentor.common.vo.OptionsVO;
import com.jeehentor.common.vo.Result;
import com.jeehentor.dto.IdsRequestDTO;
import com.jeehentor.dto.QueryDictDTO;
import com.jeehentor.dto.QueryDictItemDTO;
import com.jeehentor.entity.SysDict;
import com.jeehentor.entity.SysDictItem;
import com.jeehentor.system.service.ISysDictItemService;
import com.jeehentor.system.service.ISysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author wzd
 * @since 2024-04-29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dict")
@Tag(name = "数据字典")
public class SysDictController {

    private final ISysDictService dictService;

    private final ISysDictItemService dictItemService;



    @Operation(summary = "新建字典")
    @PostMapping("/saveDict")
    @SystemLogAnnotation(optModul = "数据字典",optType = "save",optParam = "#{sysDict}",optDesc = "新建字典")
    public Result<String> saveDict(@RequestBody SysDict sysDict){
        dictService.saveDict(sysDict);
        return Result.ok("保存成功");
    }


    @Operation(summary = "编辑字典")
    @PostMapping("/updateDict")
    @SystemLogAnnotation(optModul = "数据字典",optType = "update",optUpdateParam = "#{sysDict}",optDesc = "编辑字典")
    public Result<String> updateDict(@RequestBody SysDict sysDict){
        dictService.updateDict(sysDict);
        return Result.ok("修改成功");
    }


    @Operation(summary = "删除字典")
    @PostMapping("/delDict")
    @Transactional(rollbackFor = Exception.class)
    @SystemLogAnnotation(optModul = "数据字典",optType = "del",optParam = "#{sysDict.id}",optDesc = "删除字典")
    public Result<String> delDict(@RequestBody SysDict sysDict){
        dictService.delDict(sysDict);
        return Result.ok("删除成功");
    }

    @Operation(summary = "字典列表(分页)")
    @PostMapping("/pageList")
    public Result<Page<SysDict>> pageList(@RequestBody QueryDictDTO queryDictDTO){
        LambdaQueryWrapper<SysDict> dictLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dictLambdaQueryWrapper.like(StringUtils.isNotBlank(queryDictDTO.getDictName()),SysDict::getDictName, queryDictDTO.getDictName());
        dictLambdaQueryWrapper.like(StringUtils.isNotBlank(queryDictDTO.getDictCode()),SysDict::getDictCode, queryDictDTO.getDictCode());
        Page<SysDict> page = dictService.page(queryDictDTO.getPage(), dictLambdaQueryWrapper);
        return Result.OK(page);
    }

    @Operation(summary = "字典列表(下拉)")
    @PostMapping("/optionsList")
    public Result<List<OptionsVO>> optionsList(@RequestBody QueryDictDTO queryDictDTO){
        LambdaQueryWrapper<SysDict> dictLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dictLambdaQueryWrapper.like(StringUtils.isNotBlank(queryDictDTO.getDictName()),SysDict::getDictName, queryDictDTO.getDictName());
        dictLambdaQueryWrapper.like(StringUtils.isNotBlank(queryDictDTO.getDictCode()),SysDict::getDictCode, queryDictDTO.getDictCode());
        List<SysDict> list = dictService.list(dictLambdaQueryWrapper);
        ArrayList<OptionsVO> optionsVOS = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            list.forEach(x->{
                OptionsVO optionsVO = new OptionsVO();
                optionsVO.setLabel(x.getDictName());
                optionsVO.setValue(x.getDictCode());
                optionsVOS.add(optionsVO);
            });
        }
        return Result.OK(optionsVOS);
    }

    @Operation(summary = "获取字典项")
    @PostMapping("/getDictItemByCode")
    public Result<List<SysDictItem>> getDictItemByCode(@RequestBody QueryDictDTO queryDictDTO){
        return Result.OK(dictService.getDictItemByCode(queryDictDTO.getDictCode()));
    }

    @Operation(summary = "批量删除字典")
    @PostMapping("/batchDelDict")
    @Transactional(rollbackFor = Exception.class)
    @SystemLogAnnotation(optModul = "数据字典",optType = "del",optParam = "#{ids}",optDesc = "删除字典")
    public Result<String> batchDelDict(@RequestBody IdsRequestDTO ids){
        dictService.batchDelDict(ids.getIds());
        return Result.ok("删除成功");
    }

    @Operation(summary = "新建字典项")
    @PostMapping("/saveDictItem")
    public Result<String> saveDictItem(@RequestBody SysDictItem sysDictItem){
        dictService.saveDictItem(sysDictItem);
        return Result.ok("保存成功");
    }


    @Operation(summary = "字典项列表(分页)")
    @PostMapping("/itemPageList")
    public Result<Page<SysDictItem>> pageList(@RequestBody QueryDictItemDTO queryDictItemDTO){
        LambdaQueryWrapper<SysDictItem> dictItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dictItemLambdaQueryWrapper.eq(SysDictItem::getDictId, queryDictItemDTO.getDictId());
        dictItemLambdaQueryWrapper.like(StringUtils.isNotBlank(queryDictItemDTO.getItemText()), SysDictItem::getItemText, queryDictItemDTO.getItemText());
        dictItemLambdaQueryWrapper.orderByAsc(SysDictItem::getSortOrder);
        Page<SysDictItem> page = dictItemService.page(queryDictItemDTO.getPage(), dictItemLambdaQueryWrapper);
        return Result.OK(page);
    }

    @Operation(summary = "删除字典项")
    @PostMapping("/delDictItem")
    public Result<String> delDictItem(@RequestBody SysDictItem sysDictItem){
        dictService.delDictItem(sysDictItem);
        return Result.ok("删除成功");
    }

    @Operation(summary = "修改字典项")
    @PostMapping("/updateDictItem")
    public Result<String> updateDictItem(@RequestBody SysDictItem sysDictItem){
        dictService.updateDictItem(sysDictItem);
        return Result.ok("修改成功");
    }

}
